<template>
  <van-address-edit
    :area-list="areaList"
    :address-info="addresseditinfo"
    show-postal
    show-delete
    show-set-default
    show-search-result
    :search-result="searchResult"
    :area-columns-placeholder="areacolumnsplaceholder"
    @save="onSave"
    @delete="onDelete"
    @change-detail="onChangeDetail"
  />
</template>

<script>
  import request, { getUrl } from "../../utils/request";

  	import { areaList } from '@vant/area-data';
    import {removeStorageToken,getStorageUser,setStorageUser} from "@/utils/storage.js"//导入
import { showDlg } from "../../utils/tools";
  export default{
    data() {
      return{
        addressid:-1,
        areaList,
        addresseditinfo:{//编辑器的数据
          name:"测试数据",
          tel:"13542659896",
          areaCode:"710207",//地区编码
          province:"台湾省",
          city:"高雄市",
          county:"前镇区",
          addressDetail:"解放路1号",
          postalCode:"315000",//邮政编码
          isDefault:false
        }
      }
    },
    created() {
      this.addressid=this.$route.query.addressid//获取前一页面传入的id
      request({//页面加载的时候 发起请求
        url:getUrl("User/getUserAddDetail"),//URL
        method:"get",//请求方法
        params:{
          addressid: this.addressid
        }
      }).then(res=>{//成功回调
      if(res.data.code==0){
        var data=res.data.data
        this.addresseditinfo.name=data.uname;
        this.addresseditinfo.tel=data.tel;
        this.addresseditinfo.areaCode=data.areacode;
        this.addresseditinfo.addressDetail=data.address;
        this.addresseditinfo.province=data.province;
        this.addresseditinfo.city=data.city;
        this.addresseditinfo.county=data.county;
        this.addresseditinfo.postalCode=data.code//设置邮政编码
        if(data.isdef==1){//判定是否默认
          this.addresseditinfo.isDefault=true
        }
      }else{
        showDlg('错误提醒','数据获取失败！')
      }

      }).catch(res=>{//失败回调
        showDlg('错误提醒','网络错误！')
      })
    },
    methods:{
      onSave(val){//保存
        console.log(val)
        var isdef=0;
        if(val.isDefault==true){//将isDefault改成后端需要的值
          isdef=1;
        }
        var that = this;
        request({//页面加载的时候 发起请求
          url:getUrl("User/changeUserAdd"),//URL
          method:"post",//请求方法
          headers: {
                  'Content-Type': 'application/x-www-form-urlencoded'
          },
          data:{
            uname:val.name,
            tel:val.tel,
            province:val.province,
            city:val.city,
            county:val.county,
            areacode:val.areaCode,
            address:val.addressDetail,
            isdef:isdef,
            addressid:that.addressid,
            code:val.postalCode,
            uid:getStorageUser().id
          }
        }).then(res=>{
          if(res.data.code==0){
            showDlg("操作提示","修改成功！")
          }else{
            showDlg("操作提示","修改失败！")
          }
        })
        .catch(error=>{
            showDlg("操作提示","修改失败！")
        })
      },
      onDelete(){
        request({//页面加载的时候 发起请求
          url:getUrl("User/delAddress"),//URL
          method:"post",//请求方法
          headers: {
                  'Content-Type': 'application/x-www-form-urlencoded'
          },
          data:{
            addressid: this.addressid
          }
          }).then(res=>{
            if(res.data.code==0){
              showDlg("操作提示","删除成功！")
              this.$router.push("/addresslist")
            }else{
              showDlg("操作提示","删除失败！")
            }
          }).catch(error=>{
            showDlg("操作提示","删除失败！")
          })
      }
    }

  }
</script>

<style>
</style>
